package flc.ast.adapter;

import android.content.Intent;
import android.view.View;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.blankj.utilcode.util.ImageUtils;
import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.chad.library.adapter.base.provider.BaseItemProvider;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;

import java.util.ArrayList;
import java.util.List;

import flc.ast.R;
import flc.ast.bean.MyAlbumBean;
import flc.ast.bean.MyImgBean;
import flc.ast.bean.MyImgItemBean;
import flc.ast.bean.MyPicAlbumBean;
import stark.common.basic.adaptermutil.StkProviderMultiAdapter;
import stark.common.basic.adaptermutil.StkSingleSpanProvider;
import stark.common.basic.utils.MediaUtil;
import stark.common.basic.utils.TimeUtil;
import stark.common.basic.view.container.StkRecycleView;

public class SelPictureItemAdapter extends StkProviderMultiAdapter<MyPicAlbumBean> {

    public SelPictureItemAdapter() {
        addItemProvider(new StkSingleSpanProvider<>(800));
        addItemProvider(new SelPictureItemProvider());
    }

    private boolean isEdit = false;
    private boolean isAll = false;

    public void setAll(boolean all) {
        isAll = all;
    }

    public void setEdit(boolean edit) {
        isEdit = edit;
    }

    private class SelPictureItemProvider extends BaseItemProvider<MyPicAlbumBean> {
        @Override
        public int getItemViewType() {
            return StkProviderMultiAdapter.TYPE_VIEW;
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_sel_picture_item;
        }

        @Override
        public void convert(@NonNull BaseViewHolder baseViewHolder, MyPicAlbumBean bean) {
            if (bean.getList() == null || bean.getList().size() == 0) return;
            if (isAll) {
                for (MyImgItemBean myImgItemBean : bean.getList()) {
                    myImgItemBean.setSelect(true);
                }
            }
            switch (bean.getList().size()) {
                case 1:
                    baseViewHolder.getView(R.id.llSelPictureItemView1).setVisibility(View.VISIBLE);
                    baseViewHolder.getView(R.id.rlSelPicture1).setVisibility(View.VISIBLE);
                    baseViewHolder.getView(R.id.rlSelPicture2).setVisibility(View.INVISIBLE);
                    baseViewHolder.getView(R.id.rlSelPicture3).setVisibility(View.INVISIBLE);
                    setViewData(baseViewHolder, bean.getList().get(0).getPath()
                            , R.id.ivSelPictureItemImg1, bean.getList().get(0).isSelect(),
                            R.id.ivSelPictureItemSel1);
                    break;
                case 2:
                    baseViewHolder.getView(R.id.llSelPictureItemView1).setVisibility(View.VISIBLE);
                    baseViewHolder.getView(R.id.rlSelPicture1).setVisibility(View.VISIBLE);
                    baseViewHolder.getView(R.id.rlSelPicture2).setVisibility(View.VISIBLE);
                    baseViewHolder.getView(R.id.rlSelPicture3).setVisibility(View.INVISIBLE);
                    setViewData(baseViewHolder, bean.getList().get(0).getPath()
                            , R.id.ivSelPictureItemImg1, bean.getList().get(0).isSelect(),
                            R.id.ivSelPictureItemSel1);
                    setViewData(baseViewHolder, bean.getList().get(1).getPath()
                            , R.id.ivSelPictureItemImg2, bean.getList().get(1).isSelect(),
                            R.id.ivSelPictureItemSel2);
                    break;
                case 3:
                    baseViewHolder.getView(R.id.llSelPictureItemView1).setVisibility(View.VISIBLE);
                    baseViewHolder.getView(R.id.rlSelPicture1).setVisibility(View.VISIBLE);
                    baseViewHolder.getView(R.id.rlSelPicture2).setVisibility(View.VISIBLE);
                    baseViewHolder.getView(R.id.rlSelPicture3).setVisibility(View.VISIBLE);
                    setViewData(baseViewHolder, bean.getList().get(0).getPath()
                            , R.id.ivSelPictureItemImg1, bean.getList().get(0).isSelect(),
                            R.id.ivSelPictureItemSel1);
                    setViewData(baseViewHolder, bean.getList().get(1).getPath()
                            , R.id.ivSelPictureItemImg2, bean.getList().get(1).isSelect(),
                            R.id.ivSelPictureItemSel2);
                    setViewData(baseViewHolder, bean.getList().get(2).getPath()
                            , R.id.ivSelPictureItemImg3, bean.getList().get(2).isSelect(),
                            R.id.ivSelPictureItemSel3);
                    break;
                case 4:
                    baseViewHolder.getView(R.id.llSelPictureItemView1).setVisibility(View.VISIBLE);
                    baseViewHolder.getView(R.id.llSelPictureItemView2).setVisibility(View.VISIBLE);
                    baseViewHolder.getView(R.id.rlSelPicture1).setVisibility(View.VISIBLE);
                    baseViewHolder.getView(R.id.rlSelPicture2).setVisibility(View.VISIBLE);
                    baseViewHolder.getView(R.id.rlSelPicture3).setVisibility(View.VISIBLE);
                    baseViewHolder.getView(R.id.rlSelPicture4).setVisibility(View.VISIBLE);
                    setViewData(baseViewHolder, bean.getList().get(0).getPath()
                            , R.id.ivSelPictureItemImg1, bean.getList().get(0).isSelect(),
                            R.id.ivSelPictureItemSel1);
                    setViewData(baseViewHolder, bean.getList().get(1).getPath()
                            , R.id.ivSelPictureItemImg2, bean.getList().get(1).isSelect(),
                            R.id.ivSelPictureItemSel2);
                    setViewData(baseViewHolder, bean.getList().get(2).getPath()
                            , R.id.ivSelPictureItemImg3, bean.getList().get(2).isSelect(),
                            R.id.ivSelPictureItemSel3);
                    setViewData(baseViewHolder, bean.getList().get(3).getPath()
                            , R.id.ivSelPictureItemImg4, bean.getList().get(3).isSelect(),
                            R.id.ivSelPictureItemSel4);
                    break;
                case 5:
                    baseViewHolder.getView(R.id.llSelPictureItemView1).setVisibility(View.VISIBLE);
                    baseViewHolder.getView(R.id.llSelPictureItemView2).setVisibility(View.VISIBLE);
                    baseViewHolder.getView(R.id.rlSelPicture1).setVisibility(View.VISIBLE);
                    baseViewHolder.getView(R.id.rlSelPicture2).setVisibility(View.VISIBLE);
                    baseViewHolder.getView(R.id.rlSelPicture3).setVisibility(View.VISIBLE);
                    baseViewHolder.getView(R.id.rlSelPicture4).setVisibility(View.VISIBLE);
                    baseViewHolder.getView(R.id.rlSelPicture5).setVisibility(View.VISIBLE);
                    setViewData(baseViewHolder, bean.getList().get(0).getPath()
                            , R.id.ivSelPictureItemImg1, bean.getList().get(0).isSelect(),
                            R.id.ivSelPictureItemSel1);
                    setViewData(baseViewHolder, bean.getList().get(1).getPath()
                            , R.id.ivSelPictureItemImg2, bean.getList().get(1).isSelect(),
                            R.id.ivSelPictureItemSel2);
                    setViewData(baseViewHolder, bean.getList().get(2).getPath()
                            , R.id.ivSelPictureItemImg3, bean.getList().get(2).isSelect(),
                            R.id.ivSelPictureItemSel3);
                    setViewData(baseViewHolder, bean.getList().get(3).getPath()
                            , R.id.ivSelPictureItemImg4,
                            bean.getList().get(3).isSelect(), R.id.ivSelPictureItemSel4);
                    setViewData(baseViewHolder, bean.getList().get(4).getPath()
                            , R.id.ivSelPictureItemImg5, bean.getList().get(4).isSelect(),
                            R.id.ivSelPictureItemSel5);
                    break;
                case 6:
                    baseViewHolder.getView(R.id.llSelPictureItemView1).setVisibility(View.VISIBLE);
                    baseViewHolder.getView(R.id.llSelPictureItemView2).setVisibility(View.VISIBLE);
                    baseViewHolder.getView(R.id.rlSelPicture1).setVisibility(View.VISIBLE);
                    baseViewHolder.getView(R.id.rlSelPicture2).setVisibility(View.VISIBLE);
                    baseViewHolder.getView(R.id.rlSelPicture3).setVisibility(View.VISIBLE);
                    baseViewHolder.getView(R.id.rlSelPicture4).setVisibility(View.VISIBLE);
                    baseViewHolder.getView(R.id.rlSelPicture5).setVisibility(View.VISIBLE);
                    baseViewHolder.getView(R.id.rlSelPicture6).setVisibility(View.VISIBLE);
                    setViewData(baseViewHolder, bean.getList().get(0).getPath()
                            , R.id.ivSelPictureItemImg1, bean.getList().get(0).isSelect(),
                            R.id.ivSelPictureItemSel1);
                    setViewData(baseViewHolder, bean.getList().get(1).getPath()
                            , R.id.ivSelPictureItemImg2
                            , bean.getList().get(1).isSelect(), R.id.ivSelPictureItemSel2);
                    setViewData(baseViewHolder, bean.getList().get(2).getPath()
                            , R.id.ivSelPictureItemImg3
                            , bean.getList().get(2).isSelect(), R.id.ivSelPictureItemSel3);
                    setViewData(baseViewHolder, bean.getList().get(3).getPath()
                            , R.id.ivSelPictureItemImg4
                            , bean.getList().get(3).isSelect(), R.id.ivSelPictureItemSel4);
                    setViewData(baseViewHolder, bean.getList().get(4).getPath()
                            , R.id.ivSelPictureItemImg5
                            , bean.getList().get(4).isSelect(), R.id.ivSelPictureItemSel5);
                    setViewData(baseViewHolder, bean.getList().get(5).getPath()
                            , R.id.ivSelPictureItemImg6
                            , bean.getList().get(5).isSelect(), R.id.ivSelPictureItemSel6);
                    break;
            }

        }
    }

    private void setViewData(BaseViewHolder baseViewHolder, String path, int idImg, boolean isSelect, int idSel) {
        Glide.with(getContext()).load(path).into((ImageView) baseViewHolder.getView(idImg));
        if (isEdit && isSelect) {
            baseViewHolder.getView(idSel).setVisibility(View.VISIBLE);
        } else {
            baseViewHolder.getView(idSel).setVisibility(View.GONE);
        }
    }

}
